package com.hd.assistant.biz.manager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClickedDataMoveMonitor implements Runnable{
	private final static Logger log = LoggerFactory.getLogger(ClickedDataMoveMonitor.class);
	ClickedRedirectManager clickedRedirectManager;
	private Long writeToDbInterval = 30*60*1000l;
	private Long monitorLoopInterval = 60*1000l;;

	private Long lastRunTime = System.currentTimeMillis();

	public Long getWriteToDbInterval() {
		return writeToDbInterval;
	}


	public void setWriteToDbInterval(Long writeToDbInterval) {
		this.writeToDbInterval = writeToDbInterval;
	}


	public Long getMonitorLoopInterval() {
		return monitorLoopInterval;
	}


	public void setMonitorLoopInterval(Long monitorLoopInterval) {
		this.monitorLoopInterval = monitorLoopInterval;
	}


	public ClickedDataMoveMonitor(ClickedRedirectManager clickedRedirectManager) {
		super();
		this.clickedRedirectManager = clickedRedirectManager;
	}


	public void run() {
		// 开始监控
		log.warn("ClickedDataMoveMonitor begin to monitor....");
		do{
			try {
				
				Thread.sleep(monitorLoopInterval);
			} catch (InterruptedException e) {
				//do nothing.
			}
			if(clickedRedirectManager.needToMove()){
				if(clickedRedirectManager.dumpToDB()){
					lastRunTime = System.currentTimeMillis();
					log.warn("clickedRedirectManager.dumpToDB ok...resion:clickedRedirectManager.needToMove");
				}else{
					log.warn("clickedRedirectManager.dumpToDB false...resion:clickedRedirectManager.needToMove");
				}
			}else{
				//走定时逻辑
				long current = System.currentTimeMillis();
				if(current - lastRunTime >= this.writeToDbInterval){
					if(clickedRedirectManager.dumpToDB()){
						lastRunTime = current;
						log.warn("clickedRedirectManager.dumpToDB ok...resion:clickedRedirectManager.system Time:"+current);
					}else{
						log.warn("clickedRedirectManager.dumpToDB false...resion:clickedRedirectManager.system Time:"+current);
					}
				}
			}
		}while(true);
	}

}
